package com.wswy.wzcx.ui.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;

import com.che.libcommon.api.ApiOptionalResultObserver;
import com.che.libcommon.api.BaseResult;
import com.che.libcommon.utils.ToastUtils;
import com.wswy.wzcx.R;
import com.wswy.wzcx.api.Api;
import com.wswy.wzcx.model.UserMode;

/**
 * Created by wangduheng26 on 2018/1/22.
 * wangduheng26@gmail.com
 */

public class ChangePwdFragment extends PwdFragment {
    public static final String TAG = "ChangePwdFragment";
    public static final String ARGS_OLD_PWD = "args.old_pwd";
    private String oldPwd;

    public static ChangePwdFragment newInstance(String oldPwd) {

        Bundle args = new Bundle();
        args.putString(ARGS_OLD_PWD, oldPwd);

        ChangePwdFragment fragment = new ChangePwdFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        final Bundle bundle = getArguments();
        if (bundle != null && bundle.containsKey(ARGS_OLD_PWD)) {
            oldPwd = bundle.getString(ARGS_OLD_PWD);
        }
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        editText.setHint("请输入新密码");
        tvTitle.setText("新密码");
    }

    @Override
    protected void submit() {
        final Editable text = editText.getText();
        if (TextUtils.isEmpty(oldPwd)) {
            showToast("旧密码异常");
            return;
        }
        if (!TextUtils.isEmpty(text)) {
            Api.get().changePwd(String.valueOf(text), oldPwd)
                .subscribe(new ApiOptionalResultObserver<UserMode>() {
                    @Override
                    protected void onApiResult(@Nullable UserMode s) {
                        if (s != null) {
                            showReLoginDialog();
                        } else {
                            ToastUtils.showText(R.string.unknown_exption);
                        }
                    }

                    @Override
                    protected void onApiError(@Nullable BaseResult result) {
                        toastErrorMsg(result);
                    }
                });
        }
    }

    @Override
    public String getFragmentTitle() {
        return "输入新密码";
    }
}
